<script setup lang="ts">
  import { onMounted, ref } from 'vue'
  import icons from './requireIcons.ts'
  import SvgIcon from '@/components/SvgIcon/index.vue'

  const props = defineProps({
    visible: {
      /*弹窗开启或关闭开关*/
      type: Boolean,
      required: true,
    },
    activeIcon: {
      type: String,
      required: true,
    },
  })
  const iconList = ref(icons)
  const name = ref(null)

  /*进行筛选*/
  const filterIcons = () => {
    iconList.value = icons
    console.log(name)
    if (name) {
      iconList.value = iconList.value.filter((item) => item.includes(name.value))
    }
  }

  let $emit = defineEmits(['selected'])

  const selectedIcon = (item: any) => {
    $emit('selected', item) /*将item选中的元素返回给前面的值*/
    name.value = null
    document.body.click()
  }
</script>

<script lang="ts">
  export default {
    name: 'SelectSvgIcon',
  }
</script>

<template>
  <div class="icon-body">
    <el-input
      v-model="name"
      class="icon-search"
      clearable
      placeholder="请输入图标名称"
      @clear="filterIcons"
      @input="filterIcons"
    >
      <i slot="suffix" class="el-icon-search el-input__icon" />
    </el-input>
    <div class="icon-list">
      <div class="list-container">
        <div
          v-for="(item, index) in iconList"
          class="icon-item-wrapper"
          :key="index"
          @click="selectedIcon(item)"
        >
          <div :class="['icon-item', { active: props.activeIcon === item }]" class="el-but">
            <SvgIcon :icon-class="item" style="margin-top: 7px"></SvgIcon>
            <span>{{ item }}</span>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="scss">
  .icon-body {
    width: 100%;
    padding: 10px;
    .icon-search {
      position: relative;
      margin-bottom: 5px;
    }
    .icon-list {
      height: 200px;
      overflow: auto;
      .list-container {
        display: flex;
        flex-wrap: wrap;
        .icon-item-wrapper {
          width: calc(100% / 3);
          height: 25px;
          line-height: 25px;
          cursor: pointer;
          display: flex;
          .icon-item {
            display: flex;
            max-width: 100%;
            height: 100%;
            padding: 0 5px;
            &:hover {
              background: #ececec;
              border-radius: 5px;
            }
            .icon {
              flex-shrink: 0;
            }
            span {
              display: inline-block;
              vertical-align: -0.15em;
              fill: currentColor;
              padding-left: 2px;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
            }
          }
          .icon-item.active {
            background: #ececec;
            border-radius: 5px;
          }
        }
      }
    }
  }
</style>
